	// function to convert time
	var convertTime = function(elapsedTime)
	{
	  // convert elapsed time to hour, min and seconds
	  if (parseInt(elapsedTime) > 3600) 
	  {
		elapsedTime = (Math.floor(parseInt(elapsedTime)/3600)).toString() +"hour " + (Math.floor(parseInt(elapsedTime)/60)).toString() +"min " + (parseInt(elapsedTime)%60).toString() + "s";
	  }
	  else if (parseInt(elapsedTime) > 60)
	  {
		elapsedTime = (Math.floor(parseInt(elapsedTime)/60)).toString() +"min " + (parseInt(elapsedTime)%60).toString() + "s";
	  }
	  else
	  {
		elapsedTime = elapsedTime+"s";
	  }
	  return elapsedTime;
	}

	// function to convert numbers to format with commas
	function numberWithCommas(x)
	{
	  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	}
	
	Parse.initialize("EL9rGBARwKQl3NL4HAQ2U4GxQYjXV8dDDvDDaTPe", "WQLflsIP1zoVHCZgNjpr95dtrmaqjCaCkWSbOSrU");
	
	var GameScore = Parse.Object.extend("GameScore");
	var gameScore = new GameScore();
	
	var output = "<table> <tr> <th> Place </th> <th> Name </th> <th> Score </th> <th> Stage</th> <th> Time </th>  <th bgcolor><img src=\"assets/images/coinYellow.png\"></th> <th><img src=\"assets/images/coinRed.png\"></th> <th><img src=\"assets/images/coinGreen.png\"></th> <th><img src=\"assets/images/coinBlue.png\"></th> <th><img src=\"assets/images/boots.png\"></th> <th> <img src=\"assets/images/hourglass.png\"></th> <th><img src=\"assets/images/multiplier.png\"></th> <th><img src=\"assets/images/flame.png\"></th> </tr>"

	
	var GameScore = Parse.Object.extend("GameScore");
	var query = new Parse.Query(GameScore);
	query.descending("score");
	query.limit(50);
	query.find({
	success: function(results) {
	  // Do something with the returned Parse.Object values
	  for (var i = 0; i < results.length; i++) { 
		var object = results[i];
		var name = object.get('playerName').toString();
		var score = numberWithCommas(object.get('score').toString());
		var stagesComplete = object.get('stage').toString();
		var elapsedTime = object.get('elapsedTime').toString();
		var yellowCoinsCollected = numberWithCommas(object.get('yellowCoinsCollected').toString());
		var redCoinsCollected = numberWithCommas(object.get('redCoinsCollected').toString());
		var greenCoinsCollected = numberWithCommas(object.get('greenCoinsCollected').toString());
		var blueCoinsCollected =  numberWithCommas(object.get('blueCoinsCollected').toString());
		var multipliersCollected =  numberWithCommas(object.get('multipliersCollected').toString());
		var clocksCollected =  numberWithCommas(object.get('clocksCollected').toString());
		var bootsCollected =  numberWithCommas(object.get('bootsCollected').toString());
		var firesCollected = numberWithCommas(object.get('firesCollected').toString());
				
	  //update table
	  if (i==0)
	  {
		output = output + "<tr bgcolor=#CC9900> <td>" + parseInt(i+1) +"</td> <td>" + "<a href=\"otherUser.html?user="+name+"\">"+ name + "<a/>" + "</td> <td>" + score + "</td> <td>" + stagesComplete + "</td> <td>" + convertTime(elapsedTime) + "</td><td>" + yellowCoinsCollected + "</td><td>" + redCoinsCollected + "</td><td>" + greenCoinsCollected + "</td><td>" + blueCoinsCollected + "</td><td>" + bootsCollected + "</td><td>" + clocksCollected + "</td><td>" + multipliersCollected + "</td> <td>" + firesCollected + "</td></tr>";
	  }
	  else if (i==1)
	  {
		output = output + "<tr bgcolor=#B8B894> <td>" + parseInt(i+1) +"</td> <td>" + "<a href=\"otherUser.html?user="+name+"\">"+ name + "<a/>" + "</td> <td>" + score + "</td> <td>" + stagesComplete + "</td> <td>" + convertTime(elapsedTime) + "</td><td>" + yellowCoinsCollected + "</td><td>" + redCoinsCollected + "</td><td>" + greenCoinsCollected + "</td><td>" + blueCoinsCollected + "</td><td>" + bootsCollected + "</td><td>" + clocksCollected + "</td><td>" + multipliersCollected + "</td> <td>" + firesCollected + "</td></tr>";
	  }
	  else if (i==2)
	  {
		output = output + "<tr bgcolor=#FF9900> <td>" + parseInt(i+1) +"</td> <td>" + "<a href=\"otherUser.html?user="+name+"\">"+ name + "<a/>" + "</td> <td>" + score + "</td> <td>" + stagesComplete + "</td> <td>" + convertTime(elapsedTime) + "</td><td>" + yellowCoinsCollected + "</td><td>" + redCoinsCollected + "</td><td>" + greenCoinsCollected + "</td><td>" + blueCoinsCollected + "</td><td>" + bootsCollected + "</td><td>" + clocksCollected + "</td><td>" + multipliersCollected + "</td> <td>" + firesCollected + " </td></tr>";
	  }
	  else
	  {
		output = output + "<tr> <td>" + parseInt(i+1) +"</td> <td>" + "<a href=\"otherUser.html?user="+name+"\">"+ name + "<a/>" + "</td> <td>" + score + "</td> <td>" + stagesComplete + "</td> <td>" + convertTime(elapsedTime) + "</td><td>" + yellowCoinsCollected + "</td><td>" + redCoinsCollected + "</td><td>" + greenCoinsCollected + "</td><td>" + blueCoinsCollected + "</td><td>" + bootsCollected + "</td><td>" + clocksCollected + "</td><td>" + multipliersCollected + "</td> <td>" + firesCollected + " </td> </tr>";
	  }
	  
	  }
	  output = output + "</table>";
	  document.getElementById("scoreTable").innerHTML=output;

	},
	  error: function(error) {
		alert("Error: " + error.code + " " + error.message);
	  }
	});